package com.yxw.live_vod_boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxw.live_vod_boot.dto.collection_folder.*;
import com.yxw.live_vod_boot.entity.YxOnDemandCollectionFolderEntity;
import com.yxw.live_vod_boot.mapper.OnDemandCollectionFolderMapper;
import com.yxw.live_vod_boot.service.OnDemandCollectionFolderService;
import com.yxw.live_vod_boot.utils.AssertUtil;
import com.yxw.live_vod_boot.utils.PageUtils;
import com.yxw.live_vod_boot.vo.OnDemandCollectionFolderVO;
import com.yxw.yxnet_cd_center.common.dto.PageVO;
import com.yxw.yxnet_cd_center.common.enums.ErrorCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author fangmaoxin
 * &#064;date  2023年11月16日
 */
@Slf4j
@Service
public class OnDemandCollectionFolderServiceImpl extends ServiceImpl<OnDemandCollectionFolderMapper, YxOnDemandCollectionFolderEntity> implements OnDemandCollectionFolderService {

    private static final int DEFAULT_FOLDER_STATE = 1;

    @Autowired
    private OnDemandCollectionFolderMapper folderMapper;

    /**
     * 查询用户的收藏夹列表
     */
    @Override
    public PageVO<OnDemandCollectionFolderVO> selectCollectionFolderListByUserId(Long userId, OnDemandCollectionFolderQueryDTO dto) {
        AssertUtil.isNotNull(userId, ErrorCodeEnum.COLLECTION_USER_ID_ERROR);
        Page<YxOnDemandCollectionFolderEntity> page = new Page<>(dto.getCurrPage(), dto.getPageSize());
        List<OnDemandCollectionFolderVO> list = folderMapper.selectFolderListAndVideoListByUserId(page, userId);
        return PageUtils.get(page, list);
    }

    /**
     * 修改用户的某个收藏夹信息
     */
    @Override
    @Transactional
    public int updateCollectionFolderInfoByUserId(Long userId, OnDemandCollectionFolderUpdateDTO dto) {
        LambdaQueryWrapper<YxOnDemandCollectionFolderEntity> queryWrapper = getQueryWrapper(userId, dto.getCollectionFolderId());
        YxOnDemandCollectionFolderEntity folderEntity = baseMapper.selectOne(queryWrapper);
        AssertUtil.isNotNull(folderEntity, ErrorCodeEnum.COLLECTION_FOLDER_NOT_EXIST);
        folderEntity.setCollectionFolderName(dto.getCollectionFolderName());
        folderEntity.setUpdateTime(new Date());
        return baseMapper.updateById(folderEntity);
    }

    private static LambdaQueryWrapper<YxOnDemandCollectionFolderEntity> getQueryWrapper(Long userId, Long dto) {
        AssertUtil.isNotNull(userId, ErrorCodeEnum.COLLECTION_USER_ID_ERROR);
        LambdaQueryWrapper<YxOnDemandCollectionFolderEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(YxOnDemandCollectionFolderEntity::getUserId, userId);
        queryWrapper.eq(YxOnDemandCollectionFolderEntity::getCollectionFolderId, dto);
        return queryWrapper;
    }

    /**
     * 新增某个用户的收藏夹
     */
    @Override
    @Transactional
    public int insertCollectionFolderInfoByUserId(Long userId, OnDemandCollectionFolderAddDTO dto) {
        AssertUtil.isNotNull(userId, ErrorCodeEnum.COLLECTION_USER_ID_ERROR);
        LambdaQueryWrapper<YxOnDemandCollectionFolderEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(YxOnDemandCollectionFolderEntity::getUserId, userId);
        queryWrapper.eq(YxOnDemandCollectionFolderEntity::getCollectionFolderName, dto.getCollectionFolderName());
        YxOnDemandCollectionFolderEntity folderEntity = baseMapper.selectOne(queryWrapper);
        AssertUtil.isNull(folderEntity, ErrorCodeEnum.COLLECTION_FOLDER_EXIST);
        YxOnDemandCollectionFolderEntity update = new YxOnDemandCollectionFolderEntity();
        update.setCollectionFolderName(dto.getCollectionFolderName());
        update.setState(DEFAULT_FOLDER_STATE);
        update.setCreateTime(new Date());
        update.setUserId(userId);
        return baseMapper.insert(update);
    }

    /**
     * 删除某个用户的收藏夹
     */
    @Override
    @Transactional
    public int deleteCollectionFolderInfoByUserId(Long userId, OnDemandCollectionFolderDeleteDTO dto) {
        LambdaQueryWrapper<YxOnDemandCollectionFolderEntity> queryWrapper = getQueryWrapper(userId, dto.getCollectionFolderId());
        YxOnDemandCollectionFolderEntity folderEntity = baseMapper.selectOne(queryWrapper);
        AssertUtil.isNotNull(folderEntity, ErrorCodeEnum.COLLECTION_FOLDER_NOT_EXIST);
        folderMapper.deleteVideoInFolder(userId, dto.getCollectionFolderId());
        return baseMapper.deleteById(dto.getCollectionFolderId());
    }


    /**
     * 用户添加视频到收藏夹
     */
    @Override
    @Transactional
    public int insertVideoInToCollectionFolder(Long userId, OnDemandCollectionFolderInsertVideoDTO dto) {
        AssertUtil.isNotNull(userId, ErrorCodeEnum.COLLECTION_USER_ID_ERROR);
        LambdaQueryWrapper<YxOnDemandCollectionFolderEntity> queryWrapper = getQueryWrapper(userId, dto.getCollectionFolderId());
        YxOnDemandCollectionFolderEntity folderEntity = baseMapper.selectOne(queryWrapper);
        AssertUtil.isNotNull(folderEntity, ErrorCodeEnum.COLLECTION_FOLDER_NOT_EXIST);
        // 校验用户有没有收藏当前视频，没有就报错，收藏了才能加到收藏夹
        boolean isCollection = folderMapper.checkVideoInCollectionList(userId, dto.getVideoId());
        AssertUtil.isTrue(isCollection, ErrorCodeEnum.COLLECTION_VIDEO_NO_EXISTS);
        // 校验当前视频是否添加到了当前收藏夹里面
        boolean b = folderMapper.checkVideoInCollectionFolder(userId, dto.getVideoId(), dto.getCollectionFolderId());
        AssertUtil.isFalse(b, ErrorCodeEnum.COLLECTION_FOLDER_HAS_THIS_VIDEO);
        return folderMapper.insertVideoInToCollectionFolder(userId, dto.getVideoId(), dto.getCollectionFolderId());
    }

    /**
     * 用户移出收藏夹中视频
     */
    @Override
    @Transactional
    public int deleteVideoInCollectionFolder(Long userId, OnDemandCollectionFolderDeleteVideoDTO dto) {
        AssertUtil.isNotNull(userId, ErrorCodeEnum.COLLECTION_USER_ID_ERROR);
        LambdaQueryWrapper<YxOnDemandCollectionFolderEntity> queryWrapper = getQueryWrapper(userId, dto.getCollectionFolderId());
        YxOnDemandCollectionFolderEntity folderEntity = baseMapper.selectOne(queryWrapper);
        AssertUtil.isNotNull(folderEntity, ErrorCodeEnum.COLLECTION_FOLDER_NOT_EXIST);
        return folderMapper.deleteVideoInCollectionFolder(userId, dto.getVideoIds(), dto.getCollectionFolderId());
    }

    /**
     * 用户移动收藏夹中视频到其他收藏夹
     */
    @Override
    @Transactional
    public int moveVideoToOtherCollectionFolder(Long userId, OnDemandCollectionFolderMoveVideoDTO dto) {
        AssertUtil.isNotNull(userId, ErrorCodeEnum.COLLECTION_USER_ID_ERROR);
        // 校验当前收藏夹是否存在
        LambdaQueryWrapper<YxOnDemandCollectionFolderEntity> queryWrapper = getQueryWrapper(userId, dto.getFromFolderId());
        YxOnDemandCollectionFolderEntity folderEntity = baseMapper.selectOne(queryWrapper);
        AssertUtil.isNotNull(folderEntity, ErrorCodeEnum.COLLECTION_FOLDER_NOT_EXIST);
        // 校验移动到的收藏夹是否存在
        LambdaQueryWrapper<YxOnDemandCollectionFolderEntity> queryWrapper2 = getQueryWrapper(userId, dto.getToFolderId());
        YxOnDemandCollectionFolderEntity toFolderEntity = baseMapper.selectOne(queryWrapper2);
        AssertUtil.isNotNull(toFolderEntity, ErrorCodeEnum.COLLECTION_FOLDER_NOT_EXIST);
        // 校验移动到的收藏夹夹中是否已经有要移动的视频
        boolean checked = folderMapper.checkVideoInToFolder(userId, dto.getVideoIds(), dto.getToFolderId());
        AssertUtil.isFalse(checked, ErrorCodeEnum.TO_FOLDER_HAS_THIS_VIDEO);
        // 删除当前收藏夹中的这些视频，再把视频加到要移动到的收藏夹中
        folderMapper.deleteVideoInCollectionFolder(userId, dto.getVideoIds(), dto.getFromFolderId());
        return folderMapper.insertVideoInToFromFolder(userId, dto.getVideoIds(), dto.getToFolderId());
    }


}